import 'package:flutter/material.dart';

import '../view/page.dart';

class NavigatorHelper {
  static Future<void> push(BuildContext context,
      {required BasePage page, Function(dynamic)? callback}) async {
    final result = await Navigator.push(context,
        BasePageRoute(build: (context) => page, routerName: page.pageName));
    callback?.call(result);
  }

  static void pop<T extends Object>(BuildContext context, [T? result]) {
    Navigator.pop(context, result);
  }

  static Future<void> pushAndRemoveUntil(BuildContext context,
      {required BasePage page}) async {
    await Navigator.pushAndRemoveUntil(
      context,
      BasePageRoute(build: (context) => page),
      (check) => false,
    );
  }

  static void popUntil(BuildContext context, {String? routeName}) async {
    Navigator.popUntil(context, (route) {
      if (route.settings.name == routeName) {
        return true;
      }
      return route.isFirst;
    });
  }
}
